package com.first.wchat.util;

import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;

import android.util.Log;

public class ScanUtil {
	ScanListener mListener;
	ScanUpdateListener mUpdateListener;
	boolean isRunning;
	ArrayList<InetAddress> mIA = new ArrayList<InetAddress>();
	public interface ScanListener{
		
		public void finish();
	}
	
	
	public interface ScanUpdateListener
	{
		public void findNew(InetAddress ia);
	}
	
	
	public ScanUtil()
	{
	}
	
	public void setScanListener(ScanListener l)
	{
		mListener = l;
	}
	
	public void setScanUpdateListener(ScanUpdateListener l)
	{
		mUpdateListener = l;
	}
	
	public void stop()
	{
		isRunning =false;
	}
	public void scan()
	{

		mIA.clear();
		isRunning = true;
    	new Thread(new Runnable()
    	{

			@Override
			public void run() {
				mIA = pingAllIPs();

				if(mListener!=null)
					mListener.finish();
			}
    		
    	}).start();

	}
	
	private ArrayList<InetAddress> pingAllIPs()
	{
		String localIp = getLocalIP();
		String subCode = localIp.substring(0, localIp.lastIndexOf('.')+1);
		ArrayList<InetAddress> ips = new ArrayList<InetAddress>();
		for(int i=0;i<256;i++)
		{
			if(!isRunning)
				break;
			
			InetAddress ia = null;  
			try {
				ia = InetAddress.getByName(subCode+i);
				try {
					boolean res = ia.isReachable(500);
					if(res)
					{
						ips.add(ia);
						if(mUpdateListener!=null)
							mUpdateListener.findNew(ia);
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			} catch (UnknownHostException e) {
				ia = null;
				e.printStackTrace();
			}
				
		}
			
		return ips;
	}
	
	public ArrayList<InetAddress> getDevices()
	{
		return mIA;
	}
	
	public static InetAddress getLocalAddress()
	{
		try {
			for (Enumeration<NetworkInterface> en = NetworkInterface
					.getNetworkInterfaces(); en.hasMoreElements();) {
				NetworkInterface intf = en.nextElement();
				for (Enumeration<InetAddress> enumIpAddr = intf
						.getInetAddresses(); enumIpAddr.hasMoreElements();) {
					InetAddress inetAddress = enumIpAddr.nextElement();

					if (!inetAddress.isAnyLocalAddress()
							&& !inetAddress.isLinkLocalAddress()
							&& !inetAddress.isLinkLocalAddress()
							&& !inetAddress.isLoopbackAddress()) {

						return inetAddress;
					}

				}
			}

		} catch (SocketException e) {
			e.printStackTrace();
		}

		return null;
	}
	public static String getLocalIP()
	{

		try {
			for (Enumeration<NetworkInterface> en = NetworkInterface
					.getNetworkInterfaces(); en.hasMoreElements();) {
				NetworkInterface intf = en.nextElement();
				for (Enumeration<InetAddress> enumIpAddr = intf
						.getInetAddresses(); enumIpAddr.hasMoreElements();) {
					InetAddress inetAddress = enumIpAddr.nextElement();

					if (!inetAddress.isAnyLocalAddress()
							&& !inetAddress.isLinkLocalAddress()
							&& !inetAddress.isLinkLocalAddress()
							&& !inetAddress.isLoopbackAddress()) {

						return inetAddress.getHostAddress().toString();
					}

				}
			}

		} catch (SocketException e) {
			e.printStackTrace();
		}

		return null;
	}
	
	 public String getLocalInternetIp()
 {
		String localip = null;
		String netip = null;
		try {
			Enumeration<NetworkInterface> netInterfaces = NetworkInterface
					.getNetworkInterfaces();
			InetAddress ip = null;
			boolean finded = false;
			while (netInterfaces.hasMoreElements() && !finded) {
				NetworkInterface ni = netInterfaces.nextElement();
				Enumeration<InetAddress> address = ni.getInetAddresses();
				while (address.hasMoreElements()) {
					ip = address.nextElement();
					if (!ip.isSiteLocalAddress() && !ip.isLoopbackAddress()
							&& ip.getHostAddress().indexOf(":") == -1) {
						netip = ip.getHostAddress();
						finded = true;
						break;
					} else if (ip.isSiteLocalAddress()
							&& !ip.isLoopbackAddress()
							&& ip.getHostAddress().indexOf(":") == -1) {
						localip = ip.getHostAddress();
					}
				}
			}
		} catch (SocketException e) {
			e.printStackTrace();
		}
		if (netip != null && !"".equals(netip)) {
			return netip;
		} else {
			return localip;
		}
	}
	
}
